package com.hhq.sso.system.controller;

import java.util.List;
import java.util.Map;

import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hhq.sso.auth.base.pojo.SystemUserInfo;
import com.hhq.sso.auth.base.util.SystemUserContext;
import com.hhq.sso.common.api.CommonResult;
import com.hhq.sso.common.page.Condition;
import com.hhq.sso.system.entity.Dept;
import com.hhq.sso.system.service.IDeptService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;

/**
 * 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@RequestMapping("/dept")
@Tag(name = "部门", description = "部门")
public class DeptController {

    private IDeptService deptService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @Operation(summary = "详情", description = "传入dept")
    public CommonResult<Dept> detail(Dept dept) {
        Dept detail = deptService.getOne(Condition.getQueryWrapper(dept));
        return CommonResult.data(detail);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    @Parameters({
        @Parameter(name = "deptName", description = "部门名称", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
        @Parameter(name = "fullName", description = "部门全称", in = ParameterIn.QUERY, schema = @Schema(type = "string"))})
    @Operation(summary = "列表", description = "传入dept")
    public CommonResult<List<Dept>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> dept) {
        SystemUserInfo systemUserInfo = SystemUserContext.getSystemUserInfo();
        QueryWrapper<Dept> queryWrapper = Condition.getQueryWrapper(dept, Dept.class);
        List<Dept> list = deptService.list((!systemUserInfo.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID))
            ? queryWrapper.lambda().eq(Dept::getTenantId, systemUserInfo.getTenantId()) : queryWrapper);
        return CommonResult.data(list);
    }

    /**
     * 获取部门树形结构
     *
     * @return
     */
    @GetMapping("/tree")
    @Operation(summary = "树形结构", description = "树形结构")
    public CommonResult<List<Dept>> tree(String tenantId) {
        SystemUserInfo systemUserInfo = SystemUserContext.getSystemUserInfo();
        List<Dept> tree = deptService.tree(Func.toStr(tenantId, systemUserInfo.getTenantId()));
        return CommonResult.data(tree);
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @Operation(summary = "新增或修改", description = "传入dept")
    public CommonResult<Boolean> submit(@Valid @RequestBody Dept dept) {
        return CommonResult.status(deptService.submit(dept));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @Operation(summary = "删除", description = "传入ids")
    public CommonResult<Boolean> remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
        return CommonResult.status(deptService.removeByIds(Func.toLongList(ids)));
    }

}
